package com.china08.yxyapi.api.manage.par.study.weikecheng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.entity.mg.user.YxyUserFavor;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyUserFavorRepository;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;

/**
 * 家长 微课中心---我的收藏
 * 
 */
@CrossOrigin
@RestController("ParStudyWeikechengSrhController")
public class StudyWeikechengSrhController extends YxyApiBaseController {

    @Autowired
    private YxyWeikeService weikeService;

    @Autowired
    private YxyUserFavorRepository favorRepository;

    @RequestMapping(value = "/api_yw/manage/par/study/weikecheng", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) {

        String userId = getProfile(request, response).getId();
        // String schoolId = getProfile(request, response).getSchoolId();
        // String roleId = getProfile(request, response).getRoleId();
        //
        // // 用户的角色 使用云校接口
        // String url = YX_API_PATH + "/comm/user/role/" + userId + "/" + roleId
        // + "/" + schoolId;
        // RestTemplate rt = new RestTemplate();
        // String userRoleId = rt.getForObject(url, String.class);

        // 排序
        Pageable pageable = req.getPageable(Direction.fromString(req.sortType), "createdDate");
        Page<YxyUserFavor> page = null;
        if (StringUtils.isNotBlank(req.stage) && StringUtils.isNotBlank(req.subject)
                && StringUtils.isNotBlank(req.keyword)) {
            // 学段+学科+关键字
            page = favorRepository.findByUserIdAndResTypeAndPhaseAndSubjectAndResNameContaining(userId, "01", req.stage,
                    req.subject, req.keyword, pageable);
        } else if (StringUtils.isNotBlank(req.stage) && StringUtils.isNotBlank(req.subject)
                && StringUtils.isBlank(req.keyword)) {
            // 学段+学科
            page = favorRepository.findByUserIdAndResTypeAndPhaseAndSubject(userId, "01", req.stage, req.subject,
                    pageable);
        } else if (StringUtils.isNotBlank(req.stage) && StringUtils.isNotBlank(req.keyword)
                && StringUtils.isBlank(req.subject)) {
            // 学段+关键字
            page = favorRepository.findByUserIdAndResTypeAndPhaseAndResNameContaining(userId, "01", req.stage,
                    req.keyword, pageable);
        } else if (StringUtils.isNotBlank(req.subject) && StringUtils.isNotBlank(req.keyword)
                && StringUtils.isBlank(req.stage)) {
            // 学科+关键字
            page = favorRepository.findByUserIdAndResTypeAndSubjectAndResNameContaining(userId, "01", req.subject,
                    req.keyword, pageable);
        } else if (StringUtils.isBlank(req.subject) && StringUtils.isBlank(req.keyword)
                && StringUtils.isNotBlank(req.stage)) {
            // 学段
            page = favorRepository.findByUserIdAndResTypeAndPhase(userId, "01", req.stage, pageable);
        } else if (StringUtils.isBlank(req.stage) && StringUtils.isBlank(req.subject)
                && StringUtils.isNotBlank(req.keyword)) {
            // 关键字
            page = favorRepository.findByUserIdAndResTypeAndResNameContaining(userId, "01", req.keyword, pageable);
        } else {
            // 默认全部
            page = favorRepository.findByUserIdAndResType(userId, "01", pageable);
        }

        // 响应模型
        List<RespModel> list = new ArrayList<RespModel>();
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (YxyUserFavor yxyUserFavor : page) {
            ElasticYxyResResource resResource = weikeService.findOneResource(yxyUserFavor.getResId());
            respModel = new RespModel();
            respModel.cover = resResource.getCover();
            respModel.weikeId = resResource.getId();
            respModel.title = resResource.getTitle();
            respModel.favTime = sdFormat
                    .format(yxyUserFavor.getCreatedDate() == null ? new Date() : yxyUserFavor.getCreatedDate());

            // 相关知识点
            String knowNames = resResource.getStageName() + "/" + resResource.getWeikeSubjectName() + "/";
            String knowNameString = StringUtils.join(resResource.getKnowNames(), ",");
            respModel.knowNames = knowNames + knowNameString;
            respModel.uploaderName = resResource.getUploaderName();
            respModel.isVip = resResource.getIsVip();
            respModel.isHighQualityYx = resResource.getIsHighQualityYx();
            list.add(respModel);
        }
        Page<RespModel> pageImpl = new PageImpl<RespModel>(list, pageable, page.getTotalElements());
        return pageImpl;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String stage;// 学段

        public String subject;// 学年

        public String sortType;// 排序类型 升序asc,降序 desc

        public String keyword;// 关键字 根据微课名称

    }

    protected static class RespModel extends YxyApiRespModel {

        public String weikeId;// 微课Id

        public String cover;// 封面

        public String title;// 标题

        public String uploaderName;// 上传者

        public String favTime;// 收藏时间

        public String knowNames;// 相关知识点
        public Boolean isVip = false;

        public Integer isHighQualityYx = 0;
    }
}
